package com.eyeem.recyclerviewtools.sample.gestureview.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.alexvasilkov.android.commons.utils.Views;
import com.alexvasilkov.gestures.GestureController;
import com.alexvasilkov.gestures.commons.RecyclePagerAdapter;
import com.alexvasilkov.gestures.views.GestureImageView;
import com.eyeem.recyclerviewtools.sample.R;
import com.eyeem.recyclerviewtools.sample.data.Data;
import com.eyeem.recyclerviewtools.sample.gestureview.interface_.OnGesturePhotoListener;
import com.eyeem.recyclerviewtools.sample.gestureview.utils.GestureSettingsSetupListener;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.Arrays;

public class PaintingsPagerAdapter extends RecyclePagerAdapter<PaintingsPagerAdapter.ViewHolder> {

    private final ViewPager viewPager;
    private final Data[] paintings;
    private final GestureSettingsSetupListener setupListener;
    final int size;
    private OnGesturePhotoListener onPhotoListener;

    private PhotoPagerAdapter.OnSingleTapUpListener onSingleTapUpListener = new PhotoPagerAdapter.OnSingleTapUpListener() {
        @Override
        public void onSingleTapConfirmed(View view) {
            Log.e(PhotoPagerAdapter.class.getName(), "onGesturePhotoClick");
            Data photo = (Data) view.getTag();
            int pos = Arrays.asList(paintings).indexOf(photo);
            if (onPhotoListener != null) {
                onPhotoListener.onGesturePhotoClick(pos);
            }
        }
    };

    public PaintingsPagerAdapter(ViewPager pager, Data[] paintings,
                                 GestureSettingsSetupListener listener) {
        this.viewPager = pager;
        this.paintings = paintings;
        this.setupListener = listener;
        size = viewPager.getContext().getResources().getDisplayMetrics().widthPixels;
    }

    public PaintingsPagerAdapter(ViewPager pager, Data[] paintings,
                                 GestureSettingsSetupListener listener, OnGesturePhotoListener onPhotoListener) {
        this.viewPager = pager;
        this.paintings = paintings;
        this.setupListener = listener;
        size = viewPager.getContext().getResources().getDisplayMetrics().widthPixels;
        this.onPhotoListener = onPhotoListener;
    }

    @Override
    public int getCount() {
        return paintings.length;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup container) {
        ViewHolder holder = new ViewHolder(container).setImageClick(onSingleTapUpListener);
        holder.image.getController().enableScrollInViewPager(viewPager);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        if (setupListener != null) {
            setupListener.onSetupGestureView(holder.image);
        }
        Data photo = paintings[position];
        holder.image.setTag(photo);
        holder.progress.animate().alpha(1f);
        //GlideHelper.loadResource(paintings[position].photoUrl.replace("/h/100", "/h/" + size), holder.image);

        Picasso.with(holder.image.getContext())
                .load(photo.photoUrl.replace("/h/100", "/h/" + size))
                .noFade()
                //.placeholder(placeholder(photo.profileUrl))
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        holder.image.setImageBitmap(bitmap);
                        holder.progress.animate().cancel();
                        holder.progress.animate().alpha(0f);
                        // Re-enabling touch controls
                        if (holder.gesturesDisabled) {
                            holder.image.getController().getSettings().enableGestures();
                            holder.gesturesDisabled = false;
                        }
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {
                        holder.progress.animate().alpha(0f);
                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
    }

    public static GestureImageView getImage(RecyclePagerAdapter.ViewHolder holder) {
        return ((ViewHolder) holder).image;
    }


    static class ViewHolder extends RecyclePagerAdapter.ViewHolder {
        final GestureImageView image;
        final View progress;
        boolean gesturesDisabled;

        ViewHolder(ViewGroup container) {
            //super(new GestureImageView(container.getContext()));
            //image = (GestureImageView) itemView;
            super(Views.inflate(container, R.layout.item_photo_full));
            image = Views.find(itemView, R.id.photo_full_image);
            progress = Views.find(itemView, R.id.photo_full_progress);
        }

        ViewHolder setImageClick(final PhotoPagerAdapter.OnSingleTapUpListener onPhotoListener) {
            image.getController().setOnGesturesListener(new GestureController.OnGestureListener() {
                @Override
                public void onDown(@NonNull MotionEvent e) {

                }

                @Override
                public void onUpOrCancel(@NonNull MotionEvent e) {

                }

                @Override
                public boolean onSingleTapUp(@NonNull MotionEvent e) {
                    return false;
                }

                @Override
                public boolean onSingleTapConfirmed(@NonNull MotionEvent e) {
                    if (onPhotoListener != null) {
                        onPhotoListener.onSingleTapConfirmed(image);
                    }
                    return true;
                }

                @Override
                public void onLongPress(@NonNull MotionEvent e) {

                }

                @Override
                public boolean onDoubleTap(@NonNull MotionEvent e) {
                    return false;
                }
            });
            return this;
        }
    }

}